//1. 安装 mongoose
//2. 导入
const mongoose = require('mongoose');

//3. 连接 mongodb 服务器
mongoose.connect('mongodb://127.0.0.1:27017/project');

//4. 设置回调
mongoose.connection.on('open', () => {
    //5. 创建文档结构对象
    const BookSchema = new mongoose.Schema({
        name: String,
        author: String,
        price: Number
    });

    //6. 创建模型对象                集合名称: 创建集合时, 会自动将名字改为『复数』
    const BookModel = mongoose.model('book', BookSchema);

    //7. 批量插入
    BookModel.insertMany([
        {
            name: '红楼梦',
            author: '曹雪芹',
            price: 26.8
        },
        {
            name: '三国演义',
            author: '罗贯中',
            price: 35.6
        },
        {
            name: '水浒传',
            author: '施耐庵',
            price: 38.8
        },
    ], (err, data) => {
        if(err) throw err;
        //输出插入的数据
        console.log(data);
        //8. 关闭链接
        mongoose.connection.close();
    });

})

mongoose.connection.on('error', () => {});